package actionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import beans.Student;

public class StuManager {
	
	private static StuManager smg = new StuManager();
	
	private StuManager(){
		
	}
	
	public static StuManager getInstance(){
		if(smg == null){
			smg = new StuManager();
		}
		return smg;
	}
	
	public boolean isValidId(String id){
		return false;
	}
	
	public boolean isVailidEmail(String email){
		boolean isValid = false;
		try{
			
			JDBCConnection jdbcc = new JDBCConnection();
			Connection conn = jdbcc.getConnection();
		
			PreparedStatement ps = conn.prepareStatement("select * from student where stu_email = ?");
			ps.setString(1, email);
			
			ResultSet result = ps.executeQuery();
			
			if(result.next()){
				isValid = true;
			}
			
			conn.close();
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		return isValid;
	}
	
	public boolean findStudentById(String id){
		return false;
	}
	
	public Student getStudentById(String id){
		Student student = null;
		try{
			
			JDBCConnection jdbcc = new JDBCConnection();
			Connection conn = jdbcc.getConnection();
		
			PreparedStatement ps = conn.prepareStatement("select * from student where stu_id = ?");
			ps.setString(1, id);
			
			ResultSet result = ps.executeQuery();
			
			if(result.next()){
				String stu_id = result.getString("stu_id");
				String stu_passwd = result.getString("stu_passwd");
				String stu_name = result.getString("stu_name");
				String stu_email = result.getString("stu_email");
				String stu_phone = result.getString("stu_phone");
				String stu_gender = result.getString("stu_gender");
				student = new Student(stu_id, stu_passwd, stu_name, stu_email, stu_phone, stu_gender);
			}
			
			conn.close();
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return student;
	}
	
	public Student isValidStudent(String id, String passwd){
		Student student = getStudentById(id);
		if(student != null){
			if(!passwd.equals(student.getPasswd())){
				student = null;
			}
		}
		
		return student;
	}

}
